//
//  SettingNickNameViewController.m
//  Router
//
//  Created by teki on 15/11/6.
//  Copyright © 2015年 dc. All rights reserved.
//

#import "SettingNickNameViewController.h"
#import "CustomTextField.h"
#import "UserHelper.h"
#import "LoginResponseModel.h"
#import "MyHeaderView.h"

@interface SettingNickNameViewController ()<UITextFieldDelegate>

@property (nonatomic, strong) UIScrollView *rootScrollView;
@property (nonatomic, strong) UIView *contentView;

@property (nonatomic, strong) MyHeaderView *headerView;

@property (nonatomic, strong) CustomTextField *nickNameTextField;
@property (nonatomic, strong) UserHelper *userHelper;
@end

@implementation SettingNickNameViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.userHelper = [[UserHelper alloc] init];
    
    self.title = @"设置昵称";
    [self initContentView];
    // Do any additional setup after loading the view from its nib.
}


- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    LoginResponseModel *model =  [UserSingleton shareInstance].userModel;
    UserModel *user = model.user[0];
    self.headerView.model = user;

    
    self.nickNameTextField.textField.text = user.nickName;
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



- (void)initContentView
{
    
    CGFloat leadingPadding = 28.0f;
    CGFloat textFieldHeight = 35.0f;
    CGFloat textFieldPadding = 22;
    
    UIColor *borderColor = [UIColor colorWithHexString:@"c3c3c3"];
    
    
    //内容跟视图
    _rootScrollView=[[UIScrollView alloc]init];
    _rootScrollView.showsVerticalScrollIndicator = NO;
    _rootScrollView.backgroundColor=[UIColor clearColor];
    [self.view addSubview:_rootScrollView];
    [_rootScrollView makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
    
    
    UITapGestureRecognizer *tapGesture=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapGestureClick:)];
    [_rootScrollView addGestureRecognizer:tapGesture];
    //内容视图
    _contentView = UIView.new;
    _contentView.backgroundColor = [UIColor redColor];
    [_rootScrollView addSubview:_contentView];
    _contentView.backgroundColor = [UIColor clearColor];
    [_contentView makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(_rootScrollView);
        make.width.equalTo(_rootScrollView);
        make.height.equalTo(_rootScrollView);
    }];

    _headerView = [[MyHeaderView alloc] init];
    [_contentView addSubview:_headerView];
    [_headerView makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.equalTo(0);
        make.height.equalTo(100);
    }];
    
    
    _nickNameTextField = [[CustomTextField alloc] initWithType:CustomTextFieldTypeNone target:self FontSize:14.0f];
    [_contentView addSubview:self.nickNameTextField];
    [_nickNameTextField setStyleColor:borderColor];
    _nickNameTextField.textField.textColor = [UIColor blackColor];
    _nickNameTextField.placeholder = @"新昵称";
    [self.nickNameTextField makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_headerView.bottom).offset(20);
        make.left.equalTo(leadingPadding);
        make.right.equalTo(-leadingPadding);
        make.height.equalTo(textFieldHeight);
    }];
    
    
    
    
    UIButton *assureButton = [UIButton buttonWithType:UIButtonTypeCustom];
    assureButton.translatesAutoresizingMaskIntoConstraints = NO;
    assureButton.titleLabel.font = [UIFont systemFontOfSize:15.0f];
    [assureButton setTitle:@"确  认" forState:UIControlStateNormal];
    assureButton.layer.cornerRadius = 5.0f;
    assureButton.layer.masksToBounds = YES;
    [assureButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [assureButton setBackgroundImage:[UIImage imageWithColor:NavgationBarBackgroundColor] forState:UIControlStateNormal];
    [assureButton setBackgroundImage:[UIImage imageWithColor:RGB(78, 148, 70)] forState:UIControlStateSelected];
    [assureButton addTarget:self action:@selector(assureButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
    [_contentView addSubview:assureButton];
    [assureButton makeConstraints:^(MASConstraintMaker *make) {
//        make.top.equalTo(_nickNameTextField.bottom).offset(textFieldPadding);
        make.bottom.equalTo(_contentView.bottom).offset(-50);
        make.left.equalTo(leadingPadding);
        make.right.equalTo(-leadingPadding);
        make.height.equalTo(45);
    }];
}




#pragma mark -
#pragma mark   IBActions
- (void)tapGestureClick:(UITapGestureRecognizer *)guesture
{
    [self resignAllTextFiled];
}


- (void)resignAllTextFiled
{
    [_nickNameTextField.textField resignFirstResponder];
}

- (void)assureButtonPressed:(UIButton *)sender
{
    NSString *string = self.nickNameTextField.textField.text;
    if ([string checkEmpty]) {
        [self promptMessage:@"昵称不能为空"];
        return;
    }
    [self settingNickNameRequestWithNickName:string];
}



#pragma mark -
#pragma mark   Requests
- (void)settingNickNameRequestWithNickName:(NSString *)nickName
{
    UserModel *user = [UserSingleton shareInstance].userModel.user[0];
    self.activity = [self startActivityWithTitle:@"正在提交数据..."];
    WEAKSELF
    [self.userHelper setNickNameWithUser:user.userName userNickName:nickName success:^(BOOL succ, id responseObject, NSString *msg) {
        __strong __typeof(weakSelf)strongSelf = weakSelf;
//        if (responseObject) {
//            [strongSelf stopActivityWithActivity:strongSelf.activity title:@"修改昵称失败!"];
//        }
//        else{
            [strongSelf stopActivityWithActivity:strongSelf.activity title:@"修改昵称成功!"];
            user.nickName = nickName;
            [UserSingleton shareInstance].userModel.user = @[user];
            [[UserSingleton shareInstance] saveData];
            [strongSelf dispatchTimerWithTime:2.0 block:^{
                [strongSelf.navigationController popViewControllerAnimated:YES];
            }];
//        }
    } failure:^(NSError *error) {
        NSString *failedReason = [error localizedDescription];
        [weakSelf stopActivityWithActivity:weakSelf.activity title:failedReason];
    }];
}

@end
